import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { TypeOrmModule, TypeOrmModuleOptions } from "@nestjs/typeorm";
import { UserEntity } from "./entity/user.entity";
import { LoginController } from "./controller/login.controller";
import { LoginService } from "./service/login.service";
import { WeChatModule } from "nest-wechat";
import { WeChatModuleOptions } from "nest-wechat/dist/types";
import { UserController } from "./controller/user.controller";
import { UserService } from "./service/user.service";
import { JwtModule } from "@nestjs/jwt";
import { JwtStrategy } from "./strategy/jwt.strategy";
import { PassportModule } from "@nestjs/passport";
import { ContractEntity } from "./entity/contract.entity";
import { SigningContractEntity } from "./entity/signingContract.entity";
import { PactController } from "./controller/pact.controller";
import { PactService } from "./service/pact.service";
import { COS } from "./utils/cos";
import { UtilsController } from "./controller/utils.controller";
import { UtilsService } from "./service/utils.service";
import * as process from "process";
import { SignedContractsEntity } from "./entity/signedContracts.entity";
import { PartnersEntity } from "./entity/partners.entity";

@Module({
  imports: [
    ConfigModule.forRoot({
      ignoreEnvFile: process.env.NODE_ENV === "production"
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          type: config.get("DB_DRIVER", "mysql"),
          host: config.get("DB_HOST", "127.0.0.1"),
          port: +config.get<number>("DB_PORT", 3306),
          username: config.get("DB_USERNAME", "root"),
          password: config.get("DB_PASSWORD", "root"),
          database: config.get("DB_DATABASE", "pact"),
          entities: [UserEntity, ContractEntity, SigningContractEntity, SignedContractsEntity,PartnersEntity]
        } as TypeOrmModuleOptions;
      }
    }),
    TypeOrmModule.forFeature([UserEntity, ContractEntity, SigningContractEntity, SignedContractsEntity,PartnersEntity]),

    WeChatModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          appId: config.get("TENCENT_APP_ID"),
          secret: config.get("TENCENT_APP_SECRET")
        } as WeChatModuleOptions;
      }
    }),

    PassportModule.register({ defaultStrategy: "jwt" }),
    JwtModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          secret: config.get("JWT_SECRET"),
          signOptions: { expiresIn: +config.get("JWT_TTL", 3600) }
        };
      }
    })
  ],
  controllers: [
    LoginController,
    UserController,
    PactController,
    UtilsController
  ],
  providers: [
    LoginService,
    UserService,
    JwtStrategy,
    PactService,
    UtilsService,
    COS
  ],
  exports: []
})
export class AppModule {
}
